// 引入
const express = require('express');
const db = require('../../utils/db');

// 配置路由
const router = express.Router();

// 编辑接口
// 组织管理排序接口
router.all('/organization/sort', (req, res) => {
    let atPage = req.body.page;
    let atCount = req.body.count;
    const sql2 = `SELECT * FROM t_branch JOIN t_post ON t_branch.b_id=t_post.p_b_id ORDER BY ${req.body.sort} ${req.body.order};`;
    const sql = `SELECT * FROM t_branch JOIN t_post ON t_branch.b_id=t_post.p_b_id ORDER BY ${req.body.sort} ${req.body.order} LIMIT ${(atPage-1)*atCount},${atCount};`;
    db.query(sql, (err, data) => {
        if (err) {
            console.log(err);
            res.send({ error: 1, msg: err });
        } else {
            db.query(sql2, (err2, data2) => {
                if (err2) {
                    console.log(err2);
                    res.send({ error: 1, msg: err2 });
                } else {
                    res.send({ error: 0, msg: '排序成功', data: data, total: data2.length });
                }
            })
        }
    })
})

router.all('/class/sort', (req, res) => {
        let atPage = req.body.page;
        let atCount = req.body.count;
        const sql2 = `SELECT * FROM
    (SELECT * FROM t_department,t_major,t_class WHERE t_department.d_id=t_major.m_d_id AND t_major.m_id=t_class.c_m_id) AS t1     ORDER BY ${req.body.sort} ${req.body.order};`;
        const sql = `SELECT * FROM
    (SELECT * FROM t_department,t_major,t_class WHERE t_department.d_id=t_major.m_d_id AND t_major.m_id=t_class.c_m_id) AS t1
    ORDER BY ${req.body.sort} ${req.body.order} LIMIT ${(atPage-1)*atCount},${atCount};`;
        db.query(sql, (err, data) => {
            if (err) {
                console.log(err);
                res.send({ error: 1, msg: err });
            } else {
                db.query(sql2, (err2, data2) => {
                    if (err2) {
                        console.log(err2);
                        res.send({ error: 1, msg: err2 });
                    } else {
                        res.send({ error: 0, msg: '排序成功', data: data, total: data2.length });
                    }
                })
            }
        })
    })
    // 导出
module.exports = router;